home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Textures SDK / common / wadlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-02  |  6.0 KB  |  340 lines

  1. /***
  2. *
  3. *    Copyright (c) 1998, Valve LLC. All rights reserved.
  4. *    
  5. *    This product contains software technology licensed from Id 
  6. *    Software, Inc. ("Id Technology").  Id Technology (c) 1996 Id Software, Inc. 
  7. *    All Rights Reserved.
  8. *
  9. ****/
  10.  
  11. // wad2lib.c
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <errno.h>
  17. #include <ctype.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. //#include <sys/file.h>
  21. #include <stdarg.h>
  22.  
  23. #ifdef NeXT
  24. #include <libc.h>
  25. #endif
  26. #include "cmdlib.h"
  27. #include "wadlib.h"
  28.  
  29. /*
  30. ============================================================================
  31.  
  32.                             WAD READING
  33.  
  34. ============================================================================
  35. */
  36.  
  37.  
  38. lumpinfo_t        *lumpinfo;        // location of each lump on disk
  39. int                numlumps;
  40.  
  41. wadinfo_t        header;
  42. FILE            *wadhandle;
  43.  
  44.  
  45. /*
  46. ====================
  47. W_OpenWad
  48. ====================
  49. */
  50. void W_OpenWad (char *filename)
  51. {
  52.     lumpinfo_t        *lump_p;
  53.     unsigned        i;
  54.     int                length;
  55.     
  56. //
  57. // open the file and add to directory
  58. //    
  59.     wadhandle = SafeOpenRead (filename);
  60.     SafeRead (wadhandle, &header, sizeof(header));
  61.  
  62.     if (strncmp(header.identification,"WAD2",4) &&
  63.         strncmp(header.identification, "WAD3", 4))
  64.         Error ("Wad file %s doesn't have WAD2/WAD3 id\n",filename);
  65.         
  66.     header.numlumps = LittleLong(header.numlumps);
  67.     header.infotableofs = LittleLong(header.infotableofs);
  68.  
  69.     numlumps = header.numlumps;
  70.  
  71.     length = numlumps*sizeof(lumpinfo_t);
  72.     lumpinfo = malloc (length);
  73.     lump_p = lumpinfo;
  74.     
  75.     fseek (wadhandle, header.infotableofs, SEEK_SET);
  76.     SafeRead (wadhandle, lumpinfo, length);
  77.  
  78. //
  79. // Fill in lumpinfo
  80. //
  81.     
  82.     for (i=0 ; i<numlumps ; i++,lump_p++)
  83.     {
  84.         lump_p->filepos = LittleLong(lump_p->filepos);
  85.         lump_p->size = LittleLong(lump_p->size);
  86.     }
  87. }
  88.  
  89.  
  90.  
  91. void CleanupName (char *in, char *out)
  92. {
  93.     int        i;
  94.     
  95.     for (i=0 ; i<sizeof( ((lumpinfo_t *)0)->name ) ; i++ )
  96.     {
  97.         if (!in[i])
  98.             break;
  99.             
  100.         out[i] = toupper(in[i]);
  101.     }
  102.     
  103.     for ( ; i<sizeof( ((lumpinfo_t *)0)->name ); i++ )
  104.         out[i] = 0;
  105. }
  106.  
  107.  
  108. /*
  109. ====================
  110. W_CheckNumForName
  111.  
  112. Returns -1 if name not found
  113. ====================
  114. */
  115. int    W_CheckNumForName (char *name)
  116. {
  117.     char    cleanname[16];
  118.     int        v1,v2, v3, v4;
  119.     int        i;
  120.     lumpinfo_t    *lump_p;
  121.     
  122.     CleanupName (name, cleanname);
  123.     
  124. // make the name into four integers for easy compares
  125.  
  126.     v1 = *(int *)cleanname;
  127.     v2 = *(int *)&cleanname[4];
  128.     v3 = *(int *)&cleanname[8];
  129.     v4 = *(int *)&cleanname[12];
  130.  
  131. // find it
  132.  
  133.     lump_p = lumpinfo;
  134.     for (i=0 ; i<numlumps ; i++, lump_p++)
  135.     {
  136.         if ( *(int *)lump_p->name == v1
  137.         && *(int *)&lump_p->name[4] == v2
  138.         && *(int *)&lump_p->name[8] == v3
  139.         && *(int *)&lump_p->name[12] == v4)
  140.             return i;
  141.     }
  142.  
  143.     return -1;
  144. }
  145.  
  146.  
  147. /*
  148. ====================
  149. W_GetNumForName
  150.  
  151. Calls W_CheckNumForName, but bombs out if not found
  152. ====================
  153. */
  154. int    W_GetNumForName (char *name)
  155. {
  156.     int    i;
  157.  
  158.     i = W_CheckNumForName (name);
  159.     if (i != -1)
  160.         return i;
  161.  
  162.     Error ("W_GetNumForName: %s not found!",name);
  163.     return -1;
  164. }
  165.  
  166.  
  167. /*
  168. ====================
  169. W_LumpLength
  170.  
  171. Returns the buffer size needed to load the given lump
  172. ====================
  173. */
  174. int W_LumpLength (int lump)
  175. {
  176.     if (lump >= numlumps)
  177.         Error ("W_LumpLength: %i >= numlumps",lump);
  178.     return lumpinfo[lump].size;
  179. }
  180.  
  181.  
  182. /*
  183. ====================
  184. W_ReadLumpNum
  185.  
  186. Loads the lump into the given buffer, which must be >= W_LumpLength()
  187. ====================
  188. */
  189. void W_ReadLumpNum (int lump, void *dest)
  190. {
  191.     lumpinfo_t    *l;
  192.     
  193.     if (lump >= numlumps)
  194.         Error ("W_ReadLump: %i >= numlumps",lump);
  195.     l = lumpinfo+lump;
  196.     
  197.     fseek (wadhandle, l->filepos, SEEK_SET);
  198.     SafeRead (wadhandle, dest, l->size);
  199. }
  200.  
  201.  
  202.  
  203. /*
  204. ====================
  205. W_LoadLumpNum
  206. ====================
  207. */
  208. void    *W_LoadLumpNum (int lump)
  209. {
  210.     void    *buf;
  211.     
  212.     if ((unsigned)lump >= numlumps)
  213.         Error ("W_CacheLumpNum: %i >= numlumps",lump);
  214.         
  215.     buf = malloc (W_LumpLength (lump));
  216.     W_ReadLumpNum (lump, buf);
  217.     
  218.     return buf;
  219. }
  220.  
  221.  
  222. /*
  223. ====================
  224. W_LoadLumpName
  225. ====================
  226. */
  227. void    *W_LoadLumpName (char *name)
  228. {
  229.     return W_LoadLumpNum (W_GetNumForName(name));
  230. }
  231.  
  232.  
  233. /*
  234. ===============================================================================
  235.  
  236.                         WAD CREATION
  237.  
  238. ===============================================================================
  239. */
  240.  
  241. FILE        *outwad;
  242.  
  243. lumpinfo_t    outinfo[4096];
  244. int            outlumps;
  245.  
  246. short    (*wadshort) (short l);
  247. int        (*wadlong) (int l);
  248.  
  249. /*
  250. ===============
  251. NewWad
  252. ===============
  253. */
  254.  
  255. void NewWad (char *pathname, qboolean bigendien)
  256. {
  257.     outwad = SafeOpenWrite (pathname);
  258.     fseek (outwad, sizeof(wadinfo_t), SEEK_SET);
  259.     memset (outinfo, 0, sizeof(outinfo));
  260.     
  261.     if (bigendien)
  262.     {
  263.         wadshort = BigShort;
  264.         wadlong = BigLong;
  265.     }
  266.     else
  267.     {
  268.         wadshort = LittleShort;
  269.         wadlong = LittleLong;
  270.     }
  271.     
  272.     outlumps = 0;
  273. }
  274.  
  275.  
  276. /*
  277. ===============
  278. AddLump
  279. ===============
  280. */
  281.  
  282. void    AddLump (char *name, void *buffer, int length, int type, int compress)
  283. {
  284.     lumpinfo_t    *info;
  285.     int            ofs;
  286.     
  287.     info = &outinfo[outlumps];
  288.     outlumps++;
  289.  
  290.     memset (info,0,sizeof(info));
  291.     
  292.     strcpy (info->name, name);
  293.     strupr (info->name);
  294.     
  295.     ofs = ftell(outwad);
  296.     info->filepos = wadlong(ofs);
  297.     info->size = info->disksize = wadlong(length);
  298.     info->type = type;
  299.     info->compression = compress;
  300.     
  301. // FIXME: do compression
  302.  
  303.     SafeWrite (outwad, buffer, length);
  304. }
  305.  
  306.  
  307. /*
  308. ===============
  309. WriteWad
  310. ===============
  311. */
  312.  
  313. void WriteWad (int wad3)
  314. {
  315.     wadinfo_t    header;
  316.     int            ofs;
  317.     
  318. // write the lumpingo
  319.     ofs = ftell(outwad);
  320.  
  321.     SafeWrite (outwad, outinfo, outlumps*sizeof(lumpinfo_t) );
  322.         
  323. // write the header
  324.  
  325. // a program will be able to tell the ednieness of a wad by the id
  326.     header.identification[0] = 'W';
  327.     header.identification[1] = 'A';
  328.     header.identification[2] = 'D';
  329.     header.identification[3] = wad3 ? '3' : '2';
  330.     
  331.     header.numlumps = wadlong(outlumps);
  332.     header.infotableofs = wadlong(ofs);
  333.         
  334.     fseek (outwad, 0, SEEK_SET);
  335.     SafeWrite (outwad, &header, sizeof(header));
  336.     fclose (outwad);
  337. }
  338.  
  339.  
  340.